/*package site.jlopen.assets.spring;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Service;

import site.jlopen.entity.KusciaApiInfo;
import site.jlopen.kuscia.model.job.JobStatusResponeVo;
import site.jlopen.kuscia.model.task.TaskStatuPartie;
import site.jlopen.kuscia.model.task.TaskStatusResponeVo;
import site.jlopen.kuscia.service.job.AbstractJobManager;
import site.jlopen.mapper.job.KusciaJobMapper;
import site.jlopen.mapper.job.KusciaJobTaskMapper;

@Service
public class AsyncTaskService {
	private static Logger logger = LoggerFactory.getLogger(AsyncTaskService.class);

    private final TaskScheduler taskScheduler;
    
    private ScheduledFuture<?> scheduledFuture;
    
    private final AtomicBoolean taskRunning = new AtomicBoolean(false);
    
    private KusciaApiInfo apiInfo;
    
    private  Long projectId;
    
    private Long flowId;
    
    private Long jobId;
    
    private final AbstractJobManager nodeManager;
    
	private final KusciaJobMapper jobMapper;
    
	private final KusciaJobTaskMapper taskMapper;

    public AsyncTaskService(TaskScheduler taskScheduler, AbstractJobManager nodeManager, KusciaJobMapper jobMapper, KusciaJobTaskMapper taskMapper) {
        this.taskScheduler = taskScheduler;
        this.nodeManager = nodeManager;
        this.jobMapper = jobMapper;
        this.taskMapper = taskMapper;
    }

    public void startTask(KusciaApiInfo apiInfo, Long projectId, Long flowId, Long jobId) {
    	this.apiInfo = apiInfo;
    	this.projectId = projectId;
    	this.flowId = flowId;
    	this.flowId = flowId;
    	this.jobId = jobId;
        if (taskRunning.compareAndSet(false, true)) {
            // 使用PeriodicTrigger设置3秒间隔
            PeriodicTrigger trigger = new PeriodicTrigger(Duration.ofSeconds(3));
            scheduledFuture = taskScheduler.schedule(this::executeTask, trigger);
        }
    }

    private void executeTask() {
        if (taskRunning.get() == false) return;
       try {
			logger.info("正在进行任务监听：{}、{}、{}、{}、{}", apiInfo, projectId, flowId, jobId);
    	   JobStatusResponeVo jobInfo = doQuery();
			logger.info("任务监听结果： {}", jobInfo);
           if(!jobInfo.getState().equals("RUNNING")) {
               jobMapper.updateJobState(Long.valueOf(this.jobId), jobInfo.getState());
        	   // 如果任务停止了，那么再补偿查询一次
        	   doQuery();
        	   stopTask();
           }
		} catch (Exception e) {
			logger.error("查询任务状态异常：{}、{}、{}、{}、{}", apiInfo, projectId, flowId, jobId);
		}
    }
    
    private JobStatusResponeVo doQuery() {
    	JobStatusResponeVo jobInfo = nodeManager.queryJob(this.apiInfo, String.valueOf(this.jobId));
    	List<TaskStatusResponeVo> tasks = jobInfo.getTasks();
  	   for (TaskStatusResponeVo vo : tasks) {
  		   if(!"PENDING".equals(vo.getState())) {
  			 String errText = null;
  			 List<TaskStatuPartie> taskStatuParties = vo.getTaskStatuParties();
  			 if(null != taskStatuParties && taskStatuParties.size() > 0 && !"RUNNING".equals(vo.getState())) {
  	  			 errText = vo.getErrMsg();
  				 for (TaskStatuPartie partie : taskStatuParties) {
  					 if("FAILED".equals(partie.getState())) {
  						errText = errText + "\\\\n" + partie.getDomainId() + "\\\\n" + partie.getErrMsg();
  					 }
				}
  			 }
  			 try {
					if("RUNNING".equals(vo.getState())) {
					 continue;
					}
  				taskMapper.updateTaskStateById(vo.getTaskId(), vo.getState(), errText);
			} catch (Exception e) {
				logger.error("修改任务状态异常，终止了监听：{}、{}、{}、{}、{}：{}", apiInfo, projectId, flowId, jobId, e);
				stopTask();
			}
  		   }
  	   }
		return jobInfo;
    }

    public void stopTask() {
        if (taskRunning.compareAndSet(true, false) && scheduledFuture != null) {
        	 // true表示中断正在执行的任务
            scheduledFuture.cancel(true);
        }
    }
    
    public static void main(String[] args) {
		System.out.println("Failed".toUpperCase());
	}
}*/